package com.atguigu.orderService.controller;


import com.atguigu.commonUtils.Result;
import com.atguigu.commonUtils.ResultUtil;
import com.atguigu.orderService.service.PayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author Chen Wen bo
 * @since 2021-08-20
 */
@Api(tags = "支付日志模块")
@RestController
@RequestMapping("/orderService/log")
@CrossOrigin
public class PayLogController {

    @Resource
    private PayLogService payLogService;

    @ApiOperation("生成二维码")
    @GetMapping("/createNative/{orderId}")
    public Result<Map<String,Object>> createNative(@PathVariable String orderId){
        Map<String,Object> map = payLogService.createNative(orderId);
        System.out.println("返回二维码 = " + map);
        return ResultUtil.success(map);
    }

    @ApiOperation("查询订单支付状态")
    @GetMapping("/queryPayStatus/{orderId}")
    public Result<Map<String,String>> queryPayStatus(@PathVariable String orderId){
        Map<String,String> map = payLogService.queryPayStatus(orderId);
        System.out.println("返回订单状态 = " + map);
        if (map == null) {
            return ResultUtil.fail("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")) {
            //添加到支付表，更新订单表的订单状态
            payLogService.updateOrderStatus(map);
            return ResultUtil.success("支付成功");
        }
        return ResultUtil.defineSuccess(25000,null,"支付中...");
    }


}

